package com.qn.strategy.annotation;

import com.qn.strategy.ICouponDiscount;
import com.qn.strategy.factory.HandleServiceStrategyFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MessageServiceListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(HandleServiceStrategyAspect.class);
        beans.forEach((name, bean) -> {
            HandleServiceStrategyAspect typeHandler = bean.getClass().getAnnotation(HandleServiceStrategyAspect.class);
            HandleServiceStrategyFactory.register(typeHandler.value(), (ICouponDiscount) bean);
        });
    }
}
